package com.huidao.service;

import java.util.List;

import com.huidao.dao.UserDao;
import com.huidao.dao.UserDaoImpl;
import com.huidao.entity.User;

public class UserServiceImpl implements UserService {
	int pagesize = 5;
	UserDao dao = new UserDaoImpl();
	private User user;

	public String login(User user) {
		if (dao.isUserExist(user)) {
			if (dao.checkPassword(user)) {
				return "";
			} else {
				return "您输入的密码错误";
			}
		} else {
			return "您输入的用户不存在";
		}
	}

	public String register(User user) {
		if(dao.isUserExist(user)){
			return "您输入的用户已经存在";
		}else if(dao.addUser(user)){
				return "";
			}else{
				return "注册失败";
			}	
	}
	public String updateUser(User user, String username) {
		if(user.getUserName().equals(username)){
			if(dao.updateUser(user)){
				return "";
			}else{
				return "修改失败";
			}
		}else{
			return "您输入的用户名已存在";
		}
	}

	@Override
	public List<User> getUserlist(String keyName, int currentpage, long uid) {
		return dao.getListUser(keyName, currentpage, pagesize, uid);
	}

	@Override
	public int getTotalPage(String keyName, long uid) {
		int totalPage = dao.total(keyName,uid);
		if(totalPage%pagesize==0){
			return totalPage/pagesize;
		}else{
			return totalPage/pagesize+1;
		}
	}

	@Override
	public String addUser(User user) {
		// TODO Auto-generated method stub
		if(dao.addUser(user)){
			return "";
		}else{
			return "添加失败";
		}
	}

	@Override
	public String delUser(String id) {
		// TODO Auto-generated method stub
		if(dao.updateUser(user)){
			return "";
		}else{
			return "删除失败";
		}
	}
}
